﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Tanmia.Domain;
using System.Windows.Data;
using Tanmia.Core;
using Tanmia.Basis;
using Tanmia.Basis.ViewModel;

namespace Tanmia.Windows.InvoiceScenario
{
    public class InvoiceEditViewModel : BaseViewModel<Invoice>
    {
        public ICollectionView Persons { get; private set; }
        public ICollectionView Users { get; private set; }
        public ICollectionView Units { get; private set; }
        public ICollectionView Repositories { get; private set; }


        private IList<Person> Castomers { get; set; }
        private IList<Person> Merchants { get; set; }


        private PersonDomain PersonObj = new PersonDomain();
        private UserDomain UserObj = new UserDomain();
        private UnitDomain UnitObj = new UnitDomain();
        private RepositoryDomain RepositoryObj = new RepositoryDomain();

        private static InvoiceDomain InvoiceObj = new InvoiceDomain();

        public InvoiceEditViewModel()
            : base(InvoiceObj)
        {
            Castomers = PersonObj.GetsByType(PersonTypeEnum.Customer);  // C'est plus facile que votre approche :P :P
            Merchants = PersonObj.GetsByType(PersonTypeEnum.Merchant);  // this is easier than your approach :P :P

            Users = CollectionViewSource.GetDefaultView(UserObj.Gets());
            Units = CollectionViewSource.GetDefaultView(UnitObj.GetsWithSelect());
            Repositories = CollectionViewSource.GetDefaultView(RepositoryObj.GetsWithSelect());
        }

        public void UpdatePersons(int invoiceTypeIndex)
        {
            if (invoiceTypeIndex == 0 || invoiceTypeIndex == 2)
                Persons = CollectionViewSource.GetDefaultView(Castomers);
            else if (invoiceTypeIndex == 1 || invoiceTypeIndex == 3)
                Persons = CollectionViewSource.GetDefaultView(Merchants);

            RaisePropertyChanged("Persons");
        }

        public void Post()
        {
            InvoiceObj.Post(Entity);
        }
    }
}
